package com.celesea.workflow.core.v1.controller;

import com.celesea.workflow.core.entity.WorkflowPermission;
import com.celesea.workflow.core.service.WorkflowPermissionService;
import com.celesea.workflow.core.v1.controller.request.PermissionRequest;
import com.celesea.workflow.core.v1.controller.resource.PermissionResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author JackYuan
 * @create 2017-12-01 21:44
 */

@RestController
@RequestMapping("v1/workflows")
@Api(tags = "工作流配置")
public class WorkflowController {
    @Autowired
    private WorkflowPermissionService workflowPermissionService;

    @GetMapping(value = "permission")
    @ApiOperation(value = "查看工作流授权")
    public PermissionResource getPermission(@Valid Integer workflowType) {
        List<WorkflowPermission> permissions =
                workflowPermissionService.selectByWorkflow(workflowType);
        PermissionResource resource = new PermissionResource(permissions);
        resource.setWorkflowType(workflowType);
        return resource;
    }

    @PutMapping(value = "permission")
    @ApiOperation(value = "授权工作流")
    public void putPermission(@RequestBody @Valid PermissionRequest request) {
        Assert.notNull(request.getWorkflowType(),"流程类型不能为空！");
        workflowPermissionService.savePermission(request.getWorkflowType(),request.mergePermission());
    }
}
